package parser.nodes;

// ProcedureBody = Declarations 'BEGIN' StatementSequence 'END'

import codegen.AbstractDescr;
import codegen.CodeGen;
import codegen.ProcedureDescr;
import scanner.Token;

public class ProcedureBodyNode extends Node {
    private DeclarationsNode decls;
    private StatementSequenceNode stmts;
	private Token procName; 

    public ProcedureBodyNode(DeclarationsNode decls, StatementSequenceNode stmts, Token procName) {
        this.decls = decls;
        this.stmts = stmts;
		this.procName = procName;
    }
    
    @Override
    public void print() {
        p("ProcedureBody:");
        indent();
        decls.print();
        p("BEGIN");
        stmts.print();
        p("END");
        unindent();
    }

    @Override
    public void abstractPrint() {
        decls.abstractPrint();
        stmts.abstractPrint();
    }

	@Override
	public void compile(CodeGen c) throws CompileError {
		int procLabel = c.getLabel();
		c.outstr("JMP, " + procLabel);
		decls.compile(c);
		c.outstr("LABEL, " + procLabel);
		int declarationSize = c.nextFreeAddress();
		c.outstr("INIT, " + declarationSize);
		c.outstr("GETSP");
		c.outstr("PUSHI, " + declarationSize);
		c.outstr("ADD");
		c.outstr("SETSP");
		stmts.compile(c);
		c.outstr("GETSP");
		c.outstr("PUSHI, " + declarationSize);
		c.outstr("SUB");
		c.outstr("SETSP");
		c.outstr("REDUCE, " + declarationSize);
	}
	
	
}
